package com.test.springbootmybatis.controller;

import com.test.springbootmybatis.model.User;
import com.test.springbootmybatis.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    //    @RequestMapping (value = "/add", produces = {"application/json;charset=UTF-8"})写成这样也可以
    @PostMapping(value = "/add", produces = {"application/json;charset=UTF-8"})
    public int addUser(User user) {
        return userService.addUser(user);
    }

    //    @RequestMapping (value = "/users/{pageNum}/{pageSize}", produces = {"application/json;charset=UTF-8"})
    @GetMapping(value = "/users/{pageNum}/{pageSize}", produces = {"application/json;charset=UTF-8"})
    public Object finAllUser(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize) {
        return userService.findAllUser(pageNum, pageSize);
    }

    @RequestMapping(value = "update/{id}", produces = {"application/json;charset=UTF-8"})
    public int updateUser(@PathVariable("id") int id, @RequestParam("name") String name, @RequestParam("sex")
            String sex) {
        User user = new User();
        user.setId(id);
        user.setUsername(name);
        user.setSex(sex);
        return userService.updateUser(user);
    }

    @RequestMapping(value = "/delete/{id}", produces = {"application/json;charset=UTF-8"})
    public int deleteUser(@PathVariable("id") int id) {
        return userService.deleteUser(id);
    }
}
